package com.base.cn.platform.os.controller.manage.main;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.course.MaterialType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.material.CouMaterial;
import com.base.cn.platform.os.entity.manage.config.SysConfig;
import com.base.cn.platform.os.service.course.agent.user.AgentUserService;
import com.base.cn.platform.os.service.course.catalog.CourseCatalogService;
import com.base.cn.platform.os.service.course.material.CouMaterialService;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.exam.record.ExamRecordService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 后台待办事项Controller
 * @author LIZHIPENG
 * @create 2018-07-24 下午 4:09
 */
@Controller
@RequestMapping("/admin/manage/work/")
public class AdminSysUserWorkBenchLocationController extends AdminBaseController {

    @Autowired
    private QaQuestionService qaQuestionService;
    @Autowired
    private CouMaterialService couMaterialService;
    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private CourseCatalogService courseCatalogService;
    @Autowired
    private ExamRecordService examRecordService;
    @Autowired
    private AgentUserService agentUserService;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 跳转待办事项页面
     * @return String
     */
    @RequestMapping("/myBacklog")
    public String myBacklog(Model model){
        SysConfig sys96kConfig = sysConfigService.getSysConfigByConfigType(ConfigName.V96K);
        SysConfig sysVideoConfig = sysConfigService.getSysConfigByConfigType(ConfigName.VLASPROVIDER);
        model.addAttribute("sys96kConfig",sys96kConfig);
        model.addAttribute("sysVideoConfig",sysVideoConfig);
        return resultPage("/manage/main/backlog/backlog");
    }

    /**
     * 查询待办事数量
     * @return Map<String,Object>
     */
    @RequestMapping("/getBacklogCount")
    @ResponseBody
    public Map<String,Object> getBacklogCount(@RequestParam(value = "isLive",required = false,defaultValue = "true") Boolean isLive){
        //今日直播的免费直播数量
        Map<String,Object> condition = new HashMap<>();
        BigDecimal courseOpenCount = new BigDecimal(0);
        if(isLive){
            condition.put("status",1);
            condition.put("openLive",1);
            courseOpenCount = courseOpenService.getCourseOpenCount(condition);
        }
        //今日直播（素材）
        condition = new HashMap<>();
        condition.put("status",2);//查询已发布的
        condition.put("typeKey", MaterialType.TYPE_KEY_LIVE.getKey());//查询直播类型的
        condition.put("nowDate",new Date());
        BigDecimal materialCount = couMaterialService.count(condition);

        //今日要上传的目录
        condition = new HashMap<>();
        condition.put("nowUpload",true);//查询今日要上传的
        BigDecimal catalogCount = courseCatalogService.getCourseCatalogCount(condition);

        //查询今日的问答列表
        condition = new HashMap<>();
        condition.put("status",1);//状态正常
        condition.put("completeStatus",1);//未完结
        condition.put("nowQa",true);//是否只查询今天
        BigDecimal questionCount = qaQuestionService.questionCount(condition);

        //试卷与作业批阅
        condition = new HashMap<>();
        condition.put("type",1);//真题类型
        condition.put("useType",1);//考试
        condition.put("marking",1);//提供阅卷
        condition.put("marked",1);//未批题的
        condition.put("status",2);//已完成的
//        BigDecimal examRecordCount = examRecordService.getExamRecordCount(condition);
        BigDecimal examRecordCount = new BigDecimal(0);

        condition.put("useType",2);//作业
//        BigDecimal classesMemberTaskCount = examRecordService.getExamRecordCount(condition);
        BigDecimal classesMemberTaskCount = new BigDecimal(0);
        BigDecimal sumCount = courseOpenCount.add(materialCount).add(catalogCount).add(questionCount).add(examRecordCount).add(classesMemberTaskCount);
        return ResultUtil.SUCCESS(sumCount);
    }

    /**
     * 加载待办事项
     * @return String
     */
    @RequestMapping("/ajaxBacklogList")
    public String ajaxBacklogList(Model model){
        //今日直播的免费直播
        Map<String,Object> condition = new HashMap<>();

        //今日要上传的目录
        condition = new HashMap<>();
        condition.put("count",10);//多最查询10条
        condition.put("nowUpload",true);//查询今日要上传的
        List<Map<String,Object>> catalogList = courseCatalogService.findCourseCatalogList(condition,false,false,false,false);
        model.addAttribute("catalogList",catalogList);

        //查询今日的问答列表
        condition = new HashMap<>();
        condition.put("status",1);//状态正常
        condition.put("completeStatus",1);//未完结
        condition.put("nowQa",true);//是否只查询今天
        condition.put("count",10);
        List<Map<String,Object>> questionList = qaQuestionService.findQuestionList(condition,false,false,false,false,false,false,false);
        model.addAttribute("questionList",questionList);


        //查询推广员
        condition = new HashMap<>();
        condition.put("count",10);//只查询10条
        condition.put("status",2);//查询待审核中
        List<Map<String,Object>> agentUserList = agentUserService.queryAgentUserList(condition,false,false,false,false);
        model.addAttribute("agentUserList",agentUserList);
        return resultPage("/manage/main/backlog/ajax-backlog");
    }

    /**
     * 加载待办事项
     * @return String
     */
    @RequestMapping("/ajaxBacklogLiveList")
    @ResponseBody
    public List<Map<String, Object>> ajaxBacklogLiveList(Model model,@RequestParam("start") String start,@RequestParam("end") String end){
        List<Map<String, Object>> backlogLiveList = couMaterialService.findBacklogLiveList(start, end);
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(backlogLiveList)){
            List<Map<String,Object>> list = backlogLiveList;
            TreeMap<String, String> sysConfigContext = sysConfigService.getSysConfigContext(ConfigName.LIVETIME);
            if(ObjectUtils.isNotEmpty(list)){
                    for(Map entity:list){
                        HashMap<String, Object> data = new HashMap<>();
                        String liveBeginTime = entity.get("liveBeginTime").toString();
                        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        LocalDateTime parse = LocalDateTime.parse(liveBeginTime,df);
                        long l = parse.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
                        long millis = System.currentTimeMillis();
                        long l1 = l - millis;
                        data.put("isOpenClient",false);
                        if(l1>0){
                            long l2 = l1 / 1000 / 60;
                            String liveTime = sysConfigContext.get("liveTime");
                            if(l2<=Integer.valueOf(liveTime)){
                                data.put("isOpenClient",true);
                            }
                        }
                        //格式话数据参数
                        data.put("start",entity.get("liveBeginTime"));
                        data.put("end",entity.get("liveEndTime"));
                        data.put("materialId",entity.get("id"));
                        data.put("livebilling",entity.get("liveBilling"));
                        data.put("roomid",entity.get("roomId"));
                        data.put("ptcode",entity.get("studentCode"));
                        data.put("type",entity.get("playType"));
                        Map teacher = (Map) entity.get("teacher");
                        data.put("sign",teacher.get("id"));
                        data.put("name",teacher.get("teacherName"));
                        data.put("playUrl",entity.get("playUrl"));
                        data.put("adminCode",entity.get("adminCode"));
                        data.put("teacherCode",entity.get("teacherCode"));
                        data.put("liveState",entity.get("liveState"));

                        StringBuilder title = new StringBuilder();
                        if(ObjectUtils.isNotEmpty( entity.get("course"))){
                            Map course = (Map) entity.get("course");
                            title.append( course.get("courseName")).append("-");
                        }
                        title.append( entity.get("name"));
                        data.put("title",title);
                            if(entity.get("liveState").equals(1)){
                                data.put("color","gray");
                            }
                        else if(entity.get("liveState").equals(2)||Boolean.valueOf(data.get("isOpenClient").toString()) ){
                            data.put("color","#3EC89F");
                        }else{
                            data.put("color","#3E83E5");
                        }
           maps.add(data);
                    }
                }
        }

        return maps;
    }





}
